package com.jt.service;

import com.jt.vo.ImageVO;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Service
public class FileServiceImpl implements FileService{
    @Override
    public ImageVO upload(MultipartFile file) {
        //a.jpg  jpg|png|gif
        //获取图片的名称全部转换成小写
        String fileName = file.getOriginalFilename().toLowerCase();
        if (!fileName.matches("^.+\\.(jpg|png|gif)$")){
            return null;
        }
        //2.通过判断宽高是否为图片
        try {
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
            if (width == 0 || height == 0){
                return null;
            }
        } catch (IOException e) {
            //一般条件下为了不影响代码结构，将检查异常，转换为运行时异常
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return null;
    }
}
